package org.hq.video.service;
import org.hq.video.mapper.UserMapper;
import org.hq.video.pojo.LoginUser;
import org.hq.video.pojo.WebUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 自定义 Security UserDetails
 * 系统登录用户账号密码
 * 用于数据库读取账号密码跟
 * web 页面账号密码校验
 *
 */
@Service
public class UserDateilsServiceImpl implements UserDetailsService {
    @Autowired
    UserMapper userMapper;
    @Override
    @Cacheable
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
       System.out.println("用户登录"+username);
        //获取数据库用户
          WebUser webUser = userMapper.selectUserId(username);
        //判断用户是否存在
         if (webUser==null) {
            throw new UsernameNotFoundException("用户不存在"+username);
          }
        //用户配置
    //    List<GrantedAuthority> Authorities = AuthorityUtils.commaSeparatedStringToAuthorityList(dbcusers.get(username).getGranted());
     // User users=  new User(webUser.getUserid(),webUser.getPassword(),Authorities);
        return new LoginUser(webUser);
    }
}
